// pages/test-equal-item/index.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:[],
    index:0,
    score:0,
    next:'下一题',
    correct:'???',
    change:'',
    none:0,
    buttonClicked:true,
    dati:true,
    textname:'',
    nickName: '',
    avatarUrl: '',
    rubbish_name:'',
    aaa:true
  },
  judeg(e){
    
    let name = this.data.lists[this.data.index].rubbish
    console.log(name)
    console.log(e.target.dataset.id)
   this.setData({
     none:e.target.dataset.id,
     buttonClicked:false,
     rubbish_name:name,
     aaa:true
   })
    if(e.target.dataset.name == name){
      console.log('yes')
      this.setData({
        score:this.data.score+1,
        correct:name,
        change:true
      })
    }else{
      console.log('nonono')
      this.setData({
        correct:name,
        change:false
      })
    }
  },
  nexttitle(){
    if(this.data.index == 9){
      let name
      if(this.data.score<=3){
        name = '基础不行，多学习'
      }else if(this.data.score>4 && this.data.score<=7){
        name='还不错，继续努力'
      }else if(this.data.score>7 && this.data.score<=9){
        name='你很强，实力不容置疑'
      }else if(this.data.score == 10){
        name='恭喜您，通关成功'
      }
      this.setData({
        
        dati:false,
        textname:name
      })

    }
    // if(this.data.next == '提交'){
    //   wx.navigateTo({
    //     url: '../../pages/test-equal-success/index',
    //   })
    // }
    if(this.data.index ==8){
      this.setData({
        next:'提交'
      })
    }
    if(this.data.index != 9){
      let name = this.data.rubbish_name
      if(name == ''){
        this.setData({
          aaa:false
        })
      }else{
        this.setData({
          index:this.data.index+1,
          none:false,
          buttonClicked:true,
          change:'',
          correct:'???',
          rubbish_name:''
        })
      }
      
    }
    
     
    
    
  },
  backtop(){
    if(!this.data.dati){
      const user_list = wx.getStorageSync('user_list') || [];
      console.log(this.data.score*10)
      user_list.score += this.data.score*10
      wx.setStorage({
        key: "user_list",
        data:user_list
      })
      db.collection('users').where({
        openid: user_list.openid
      }).update({
        data:{
           score:user_list.score,
            pass:user_list.pass
        },
        success(res){
          console.log("修改成功", res)
        },
        fail(res){
          console.log("修改失败", res)
        }
      })
     
    }
    wx.switchTab({
      url: '../../pages/index/index'
    })
      
     },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user = wx.getStorageSync("user_list") || [];
    this.setData({
      avatarUrl:user.image_src,
      nickName:user.name
    })
    //这里是随机获取云数据库数据
    db.collection('rubbion_list')
    .aggregate()
    .sample({
      size: 10
    })
    .end()
    .then(res=>{
      console.log(res)
       this.setData({
        lists:res.list
       })
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})